<?php

/*
 * employe/index.phtml
 */
?>
<?php
$title = "Employés";
$this->headTitle($title);

// Variables
$site	 = $this->site;
$refresh = $this->refresh;
$codes	 = $this->codes;
$codec	 = $this->codec;
?>

<div class="content-head">
    <h3>
	Liste des <?php echo $title; ?> du Site
	<a href="#" id="site" data-type="select"><?php echo $site->libelle ?></a>&nbsp;
	<img src='<?php echo $this->basePath() . '/img/rhx/refresh-16x16.png' ?>' title="Actualiser la liste" class='rhx-refresh' onclick="return refresh();">
    </h3>
    <div class="toolbar">
	<div class="toggle add" title="Ajouter">
            <a href="#" id='addNewEmployeButton' style="display: block; width: 100%; height: 100%;"></a>
        </div>
    </div>
    <form action="#" method="post" style="display: none;">
	<input type="hidden" name="site_id" id="data-site" value="<?php echo $site->id; ?>">
	<input type="hidden" name="page" id="data-page" value="<?php echo $this->page; ?>">
	<input type="hidden" name="perpage" id="data-perpage" value="<?php echo $this->perPage; ?>">
    </form>
</div>

<div class="content-body">

    <!-- CONTENU AJAX -->
    <div id='content-data'>
	<!-- AJAX LOADING -->
	<?php if (!$refresh): ?>
    	<div class="list-container site-container">
	    <?php foreach ($this->siteList as $s): ?>
    	    <div class="site-one">
		<a href="<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'index', 'id'		 => $s->id, 'other1'	 => 1, 'other2'	 => $this->page, 'other3'	 => $this->perPage)); ?>"><?php echo $s->libelle; ?></a>
	    </div>
	    <?php endforeach; ?>
	</div>
	<?php endif; ?>
    </div>

</div>

<script type="text/javascript">
    // Démarrage
    $(function(){
	// Liste des sites éditables
	$('#site').editable({
	    title : 'Liste des Sites',
	    source : '<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'jsonSiteList')) ?>',
	    validate: function(value){
		$('#data-site').val(value);
		refresh();
		changeSite(value);
	    }
	});

	// Ajouter un nouvel employé
	$('#addNewEmployeButton').click(function(e){
	    e.preventDefault();
	    siteId = $('#data-site').val();
	    window.location.href = "<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'add')) ?>/" + siteId;
	});

	// Chargement de la liste des Employés du site en cours
    <?php if ($refresh): ?>
    	refresh();
    <?php endif; ?>

    });
    
    /**
    * Rafraichir la liste des employés

     * @returns {Boolean}     */
    function refresh(){
	siteId = $('#data-site').val();
	page = $('#data-page').val();
	perpage = $('#data-perpage').val();
	ajax = $.ajax({
	    type: 'post',
	    url : '<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'loadIndex')) ?>/' + siteId + '/1/' + page + '/' + perpage
	});
	ajax.success(function(data){
	    $('#content-data').hide().html(data).fadeIn('normal');
	});
	ajax.error(function(){
	    $('#content-data').html("<div class='rh-error'>Contenu Impossible à charger</div>");
	});

	return false;
    }
</script>

